package com.wuliu.action.login;

import java.io.IOException;
import java.util.List;

import beartool.MD5;

import com.wuliu.model.sys.Menu;
import com.wuliu.action.BaseAction;
import com.wuliu.business.login.LoginObj;
import com.wuliu.business.sys.MenuObj;
import com.wuliu.model.sys.User;

public class LoginAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String securityCode;
	private String username;
	private String password;
	
	public String login(){

		
		if((User)session.get("user")!=null){
			List<Menu> mainMenus = MenuObj.getMenuListByAccount("201611100001", ((User)session.get("user")).getUserAccount());
			String result = MenuObj.getMenuResult(mainMenus, request.getContextPath(), ((User)session.get("user")).getUserAccount());
			request.setAttribute("result",result);
			return "success";
		}else{
			
			if(username==null||password==null){
				request.setAttribute("showMsg", "请输入用户名和密码！");
				return ERROR;
			}
			
//			String session_securityCode = (String)session.get("securityCode");
//			if(securityCode==null||!securityCode.equals(session_securityCode)){
//				request.setAttribute("showMsg", "验证码错误！");
//				return ERROR;
//			}
			
			MD5 md5 = new MD5();
			User vo = LoginObj.getLogin(username,md5.getMD5ofStr(password));	

			if(vo!=null){
				session.put("user", vo);
				//获取用户菜单
				List<Menu> mainMenus = MenuObj.getMenuListByAccount("201611100001", vo.getUserAccount());
				String result = MenuObj.getMenuResult(mainMenus, request.getContextPath(), vo.getUserAccount());
				request.setAttribute("result",result);
		        return "success";
			}else{
				vo	= LoginObj.getUserAccount(username);
				if(vo!=null){
					if(!vo.getUserPwd().equals(md5.getMD5ofStr(password))){
						request.setAttribute("showMsg","输入的密码错误!");
					}else{
						request.setAttribute("showMsg", "用户无登陆权限！");
					}
					
				}else{
					request.setAttribute("showMsg","请输入正确的登陆账号!");
				}					
				return ERROR;
				
			}
		}	
	}
	
	public String AjaxLogin(){
		String psn_account =  request.getParameter("psn_account");
        String psn_pwd = request.getParameter("user_pas");
        String status = "0";
        MD5 md5 = new MD5();
        
        User vo = LoginObj.getLogin(psn_account,md5.getMD5ofStr(psn_pwd));	//业务员
        
        if(vo!=null){
        	session.put("user", vo);
        	status="1";
        }
        
        response.setCharacterEncoding("utf8");
        try {
			response.getWriter().write(status);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
		return null;
	}
	public String getSecurityCode() {
		return securityCode;
	}
	public void setSecurityCode(String securityCode) {
		this.securityCode = securityCode;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

